package com.huai.demo_test1.controller;

import com.huai.demo_test1.pojo.Student;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @author 淮晓欢
 * @date 2021/12/28
 **/
@RestController
@RequestMapping("/restTemplate")
@Slf4j
public class RestTemplateController {
    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/{id}")
    public ResponseEntity<String> findById(@PathVariable Integer id){
        ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://localhost:8080/test/echo/1/淮晓欢", String.class);
        log.info("responseEntity.getStatusCode():"+responseEntity.getStatusCode());
        log.info("responseEntity.getBody():"+responseEntity.getBody());

        return responseEntity;
    }

    @PostMapping
    public ResponseEntity<String> saveStudent(@RequestBody Student student){
        ResponseEntity<String> postForEntity = restTemplate.postForEntity("http://localhost:8080/test/echo/requestBody", student, String.class);
        log.info("postForEntity.getStatusCode():"+postForEntity.getStatusCode());
        log.info("postForEntity.getBody():"+postForEntity.getBody());

        return postForEntity;
    }

    @PutMapping
    public ResponseEntity<String> updateStudent(@RequestBody Student student){
        restTemplate.put("http://localhost:8080/test/echo/requestBody",student);
        return new ResponseEntity<>("修改成功", HttpStatus.OK);
    }


}
